//------------------------------------------------------------------------------
//SFWK Framework
//Copyright (C) 2011 SQLI

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

using System;

using SFWK.Core.Unity;
using System.Diagnostics;


namespace SFWK.Core
{
    #region Enum
    /// <summary>
    /// Provides logging severity values
    /// </summary>
    [Serializable]
    public enum ELogSeverity
    {
        Debug = 0,
        Warning = 1,
        Information = 2,
        Error = 3,
        Critical = 4,
        Security = 5,
    }
    #endregion Enum

    #region interface ILogger
    /// <summary>
    /// Interface of the generic logger tool
    /// </summary>
    public interface ILogger
    {
        void Log(object source, string title, string message, ELogSeverity severity);
        void Log(object source, string title, Exception exception, ELogSeverity severity);
        void Log(object source, string title, string message, Exception exception, ELogSeverity severity);
    }

    #endregion

    #region class LogHelper
    /// <summary>
    /// Provides generic support for logging. Implementation must be provided in Unity container config section of the app/web config file
    /// </summary>
    public static class LogHelper
    {
        private static ILogger _logger;

        private static ILogger Logger
        {
            get
            {
                //System.Diagnostics.Trace.
                //System.ApplicationException
                if (_logger == null)
                {
                    _logger = ServiceProvider.GetService<ILogger>();
                }
                return _logger;
            }
        }
        /// <summary>
        /// Logs a message through the configurer logging provider
        /// </summary>
        /// <param name="source"></param>
        /// <param name="title"></param>
        /// <param name="message"></param>
        /// <param name="severity"></param>
        public static void Log(object source, string title, string message, ELogSeverity severity)
        {
            Logger.Log(source, title, message, severity);
        }

        /// <summary>
        /// Logs a message through the configurer logging provider
        /// </summary>
        /// <param name="source"></param>
        /// <param name="title"></param>
        /// <param name="exception"></param>
        /// <param name="severity"></param>
        public static void Log(object source, string title, Exception exception, ELogSeverity severity)
        {
            Logger.Log(source, title, exception, severity);
        }

        /// <summary>
        /// Logs a message through the configurer logging provider
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="severity"></param>
        public static void Log(Exception exception, ELogSeverity severity)
        {
            StackFrame frame = new StackTrace().GetFrame(1);
            Logger.Log(frame.GetType().Name, frame.GetMethod().Name, exception, severity);
        }

        public static void Log(object context, Exception exception, ELogSeverity severity)
        {
            string strContext = "";
            if (context != null)
            {
                if (context.GetType().IsSerializable)
                    strContext = SerializeHelper.SerializeXml(context);
                else
                    strContext = context.GetType().FullName;
            }
            StackFrame frame = new StackTrace().GetFrame(1);
            Logger.Log(frame.GetType().Name, frame.GetMethod().Name, strContext ,exception, severity);
        }
        

    }
    #endregion
}

